<?php

namespace app\pxs\service;

use app\admin\model\User;
use app\sys\model\UrlSourceLog;
use app\sys\model\UserActionLog;
use app\sys\model\UserActionLogToken;
use Firebase\JWT\JWT;
use Kqtec\IpLocationZh\Ip;
use think\facade\Cookie;
use think\facade\Log;
use think\facade\Request;


class JwtService
{

    // 获取token
    public static function getToken($ip,$userAgent,$mobile,$beUrl,$afUrl):string
    {
        $key = "ic_sys";  //这里是自定义的一个随机字串，应该写在config文件中的，解密时也会用，相当    于加密中常用的 盐  salt

        $no = msectime().rand(1000,9999);
        if($mobile){
            $resNo = User::where("mobile",$mobile)->value("no");
            if($resNo)$no = $resNo;
        }

        $data = [
            "no" => $no,
            "ip" => $ip,
            "user_agent" => $userAgent,
            "name"=> "客户".$no,
            "mobile"=>$mobile,
            "exp"=> strtotime("+12 hour")
        ];
        $token = Jwt::encode($data,$key,"HS256");
        // 记录

        UrlSourceLog::add($beUrl,$afUrl,$no);


        $start = Request::time(true) * 1000;
        $ip = Request::ip();
        $address = implode("/", Ip::find($ip)["location"]);
        $user = Request::param("_user");
        $user_no = "";
        $user_name = "";
        if ($user != null) {
            $user_no = $user["no"];
            $user_name = $user["name"];
        }

        $ms = msectime() - intval($start);
        $info = [
            "user_no" => $user_no,
            "user_name" => $user_name,
            "url" => Request::baseUrl(),
            "ip" => $ip,
            "address" => $address,
            "method" => Request::method(),
            "ms" => $ms,
            "request_body" => Request::except("_user,_uuid"),
            "uuid" => Request::param("_uuid"),
            "http_status" => 200,
            "action" => Request::action(),
            "user_agent" => Request::header("user-agent"),
            "token" => $token,
            "request_head" => Request::header("token"),
            "create_time" => time()
        ];
        UserActionLogToken::create($info);

        Cookie::set("ic_pxs_token",$token);
        Log::info("getToken:$mobile");
        //根据参数生成了 token
        return $token;
    }

    // 验证
    public static function check($token)
    {
//        $cookieDate = Cookie::get("ic_pxs_token");
//        if (!$cookieDate){
//            return "token不存在~";
//        }
        $cookieDate = $token;
        $key = "ic_sys";  //上一个方法中的 $key 本应该配置在 config文件中的
        $data = [];
        try {
            $data = JWT::decode($cookieDate, $key, ["HS256"]);
        }catch (\Exception $e){
            return "token已过期~";
        }
        if(isset($data) &&  isset($data->exp) && $data->exp < time()) $data = [];
        if (empty($data)){
            return "token已过期~";
        }
        if(!isset($data->exp)){
            return "token已过期~";
        }
        return "";
    }

    public static function getData($token):array
    {
//        $cookieDate = Cookie::get("ic_pxs_token");
//        if(!$cookieDate){
//            return [];
//        }
        $cookieDate = $token;
        $key = "ic_sys";  //上一个方法中的 $key 本应该配置在 config文件中的
        $data = JWT::decode($cookieDate, $key, ["HS256"]);
        return object2array($data);
    }
}